#include <objbase.h> 
#include <stdio.h>
#include <atlbase.h>
#include <relogger.h>

class TraceEventCallback : public ITraceEventCallback
{
public:

    STDMETHOD(OnEvent)(_In_ ITraceEvent* traceEvent, _In_ ITraceRelogger* relogger)
    {
        relogger->Inject(traceEvent);
        return S_OK;
    }

    STDMETHOD(OnBeginProcessTrace)(_In_ ITraceEvent *header, _In_ ITraceRelogger *relogger)
    {
        UNREFERENCED_PARAMETER(relogger);
        UNREFERENCED_PARAMETER(header);
        return S_OK;
    }

    STDMETHOD(OnFinalizeProcessTrace)(_In_ ITraceRelogger *relogger)
    {
        UNREFERENCED_PARAMETER(relogger);
        return S_OK;
    }

    STDMETHOD(QueryInterface)(_In_ REFIID riid, _Out_ PVOID* ppObj)
    {
        if (riid == __uuidof(IUnknown) || (riid == __uuidof(ITraceEventCallback)))
        {
            *ppObj = this;
        }
        else
        {
            *ppObj = NULL;
            return E_NOINTERFACE;
        }

        AddRef();
        return ERROR_SUCCESS;
    }

    STDMETHOD_(ULONG, AddRef)()
    {
        return InterlockedIncrement(&m_RefCount);
    }

    STDMETHOD_(ULONG, Release)()
    {
        LONG nRefCount;
        nRefCount=InterlockedDecrement(&m_RefCount);

        if (nRefCount == 0)
        {
            delete this;
        }

        return nRefCount;
    }

    TraceEventCallback() : m_RefCount(1) {}

    virtual ~TraceEventCallback() {}

private:
    LONG m_RefCount;
};